home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr37
/
satsfaxt.zip
/
CCPUTILS.ZIP
/
CCANCEL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-30
|
12KB
|
283 lines
/*---------------------------------------------------------------------------*
* CCANCEL.C *
*---------------------------------------------------------------------------*
* This program allows the user to cancel the current task, as well as *
* pending events. The action is not immediate; it may take up to *
* thirty seconds for the task to end. *
* *
* The following source code is intended to assist developers in *
* creating applications which support the DCA/Intel Communicating *
* Applications Specification Version 1.0A. It is provided free of charge *
* and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source *
* code may be modified, enhanced, copied and distributed with applications *
* that support CAS on a royalty free basis. *
*---------------------------------------------------------------------------*
* History: - Initial release 9/14/89. *
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <dos.h>
#include <io.h>
#include <malloc.h>
#include "cas.h" /* Intel CAS header file. */
#include "parse.h" /* Parser header file. */
#include "util.h" /* CAS utilities header file. */
int JobNo = 0; /* Cancel specified (pending) job. */
unsigned long EventNo = 0L; /* Converted job number. */
int CancelAll = FALSE; /* Cancel all pending & current jobs. */
int Verbose = FALSE; /* List job#'s as they are cancelled. */
int Confirm = FALSE; /* Confirm before cancelling. */
int Help = FALSE; /* Show help text. */
int RedirOut = FALSE; /* Redirect output to file. */
CECS *TSTATbuffer; /* Task Status buffer. */
ECF *TCFbuffer; /* Task Control Files buffer. */
FILE *ccout; /* Redirected stdout file. */
STABLE SwitchTable[] = /* Switch table for command line parser. */
{
{ 'A', BOOL, &CancelAll },
{ 'a', BOOL, &CancelAll },
{ 'C', BOOL, &Confirm },
{ 'c', BOOL, &Confirm },
{ 'E', INT, &JobNo },
{ 'e', INT, &JobNo },
{ 'N', BOOL, &Verbose },
{ 'n', BOOL, &Verbose },
{ 'R', BOOL, &RedirOut },
{ 'r', BOOL, &RedirOut },
{ '?', BOOL, &Help }
};
#define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
char *DescTable[] = /* Help function description text. */
{
"CCANCEL (V1.0) Cancels the currently executing event. CCANCEL can\n",
"also cancel pending events using the /A (all) or /E (single event)\n",
"options.\n\n",
"Syntax: CCANCEL [option...]\n\n"
};
#define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
char *HelpTable[] = /* Help function switch table. */
{
"/A Cancel all pending & current event.\n",
"/E<number> Cancel specified event.\n\n",
"/C Confirm all cancels.\n",
"/N Notify by listing events as they're canceled.\n\n",
"/R Redirect output to file OUTPUT.CC.\n",
"/? Display help information.\n"
};
#define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
/*---------------------------------------------------------------------------*
* GetTaskBuffer() *
*---------------------------------------------------------------------------*
* Get information about the specified task. The TSTATbuffer is filled with *
* info about the task on return. The handle returned by the CASOpenTaskFile *
* is a normal MS-DOS file handle. *
* *
*---------------------------------------------------------------------------*
* Parameters: int Handle - Valid event handle. *
* BYTE Queue - Which queue to search. *
* char *TCFbuffer - ECS structure filled with event *
* information. *
* int *FileHandle - Filled with DOS file handle. *
* Return: none *
*---------------------------------------------------------------------------*/
GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
{
/* Open the Task Control File and read information. */
if((*FileHandle = CASOpenFile(Handle,0,Queue)) > 0)
read(*FileHandle, TCFbuffer, sizeof(ECF));
else
CASError(CASNOTCF, TRUE, -(*FileHandle));
}
/*---------------------------------------------------------------------------*
* CancelAllJobs() *
*---------------------------------------------------------------------------*
* Cancel all tasks that are pending. If confirmation is asked on the command*
* line, then query the user before canceling. The verbose option prints a *
* message including the job number but does not query the user before *
* canceling. *
*---------------------------------------------------------------------------*
* Parameters: ECF *TCFbuffer - Unintialized ECF structure. *
* Return: none *
*---------------------------------------------------------------------------*/
CancelAllJobs(ECF *TCFbuffer)
{
int EventHandle; /* Event handle to cancel. */
/* Get the first event in the task queue. */
EventHandle = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, TASK_QUEUE);
while(EventHandle > 0) {
/* Cancel the event. */
CancelJob(EventHandle, TCFbuffer);
/* Now get the next event in the task queue. */
EventHandle = CASFindNext(TASK_QUEUE);
}
}
/*---------------------------------------------------------------------------*
* CancelJob() *
*---------------------------------------------------------------------------*
* Cancel the specified (pending) job. This is accomplished by simply *
* deleting the Task Control File. If confirmation is asked on the command *
* line, then query the user before canceling. The verbose option prints *
* a message including the job number, but does not query the user before *
* canceling. *
*---------------------------------------------------------------------------*
* Parameters: int EventHandle - Event handle of job to cancel. *
* ECF *TCFbuffer - Uninitialized ECF structure. *
* Return: none *
*---------------------------------------------------------------------------*/
CancelJob(int EventHandle, ECF *TCFbuffer)
{
int ErrorCode; /* Error returned from CAS function. */
int FileHandle; /* DOS file handle (from GetTaskBuffer). */
char answer[1]; /* Users response to confirmation quest. */
/* if requested, be verbose/confirm. */
if(Verbose | Confirm) {
/* Get information about the task and close it. */
GetTaskBuffer(EventHandle, TASK_QUEUE, (char *)TCFbuffer, &FileHandle);
close(FileHandle);
fprintf(stdout, "Event Number %d.\n", EventHandle);
if(Confirm) {
fprintf(stdout, " Cancel event (y/n)? ");
gets(answer);
}
if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
/* Delete the control file, this will in effect cancel the job. */
if((ErrorCode = CASDeleteFile(EventHandle,0,TASK_QUEUE)) == SUCCESS)
fprintf(stdout, "Pending event canceled.\n");
else
CASError(CASNOCAN, TRUE, -ErrorCode);
}
else
/* Delete the control file. */
if((ErrorCode = CASDeleteFile(EventHandle,0,TASK_QUEUE)) == SUCCESS)
;
else
CASError(CASNOCAN, TRUE, -ErrorCode);
}
/*---------------------------------------------------------------------------*
* CancelCurrent() *
*---------------------------------------------------------------------------*
* Cancels the currently executing event. If confirmation is asked on the *
* command line, query the user before canceling. The verbose option prints *
* a message but does not query the user before canceling. *
*---------------------------------------------------------------------------*
* Parameters: CECS *TSTATbuffer - An uninitialized CECS structure. *
* Return: None *
*---------------------------------------------------------------------------*/
CancelCurrent(CECS *TSTATbuffer)
{
int ErrorCode; /* Error returned from CAS function. */
int EventHandle; /* Event handle (from GetCurrentEvent). */
char answer[1]; /* Users response to confirmation quest. */
/* Get information about the task. The TSTATbuffer is a structure
* containing the Task Control File record and ONE File Transfer Record.
* This call is made to retrieve the EventHandle of the currently executing
* event. */
if(EventHandle = CASGetCurrentEventStatus(TSTATbuffer) > 0) {
if(Verbose | Confirm) {
fprintf(stdout, "Canceling current event.\n", EventHandle);
if(Confirm) {
fprintf(stdout, " Cancel job (y/n)? ");
gets(answer);
}
if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
/* Cancel the current event as specified by EventHandle. */
if((ErrorCode = CASAbortCurrentEvent()) > 0)
fprintf(stderr, "Current event canceled.\n");
else
CASError(CASNOCUR, TRUE, -ErrorCode);
}
else
/* Cancel the current event. */
if((ErrorCode = CASAbortCurrentEvent()) > 0)
;
else
CASError(CASNOCUR, TRUE, -ErrorCode);
}
else
/* If CASGetCurrentEventStatus returns FAIL, then no current event! */
if(!CancelAll)
CASError(CASNOEVENT, TRUE, 0);
}
/*---------------------------------------------------------------------------*
* MAIN() *
*---------------------------------------------------------------------------*
* Prepare to cancel the current event. Two buffers need to be created, *
* TSTAT and TCF. If a job number is specified on the command line, then *
* cancel that job. Otherwise, cancel the current job (this is the default). *
* If /A given, then cancel all current and pending jobs. *
*---------------------------------------------------------------------------*
* Return: The DOS exit code is 0 if no errors are encountered, 1 for *
* commandline syntax errors, 2 for CCAM error, and 3 for DOS *
* errors. *
*---------------------------------------------------------------------------*/
main(int argc, char **argv)
{
int result; /* DOS & CAS function return value. */
argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
/* Convert job number to long. */
EventNo = atol(JobNo);
/* If help is wanted, print info and exit (with 0 exit code). */
if(Help)
CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
/* Insure that the Resident Scheduler is installed. */
if((result = CASGetInstalledState()) != INSTALLED) {
if( result == NOTiOK )
CASError(CASNOHWO, TRUE, 0);
else
CASError(CASNOHWN, TRUE, 0);
}
/* Redirect stdout to a file. */
if(RedirOut) {
if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
CASError(CASOPENTEMP, TRUE, 0);
if(-1 == dup2(fileno(ccout), 1))
CASError(CASBADREDIR, TRUE, 0);
}
/* Get buffer for Task information. */
TSTATbuffer = (CECS *)malloc(sizeof(CECS));
if(TSTATbuffer == NULL)
CASError(CASNOMEM, TRUE, 0);
/* Get buffer for Task Control information. */
TCFbuffer = (ECF *)malloc(sizeof(ECF));
if(TCFbuffer == NULL)
CASError(CASNOMEM, TRUE, 0);
/* Cancel the specified job or the current event if none was entered
* on command line. */
if(JobNo != 0)
CancelJob(JobNo, TCFbuffer);
else if(CancelAll) {
CancelAllJobs(TCFbuffer);
CancelCurrent(TSTATbuffer);
}
else
CancelCurrent(TSTATbuffer);
exit(0);
}